var dataList = [],
	index = 'all',
	dateSelect = null;

$(() => {
	initDateSelect();
	initTags();
	loadData();
	initListener();
})

initDateSelect = () => {
	let today = getNowTime('YYYY-MM-DD');
	dateSelect = new dateMultiFunc({
		type: 2,
		minTime: '2025-01-01',
		maxTime: today,
		radius: [10, 10, 0, 0],
		backFormat: "-",
		defaultYears: today,
		appointTime: [{
			date: today,
			text: "今天"
		}],
		cancelFunc: function() {
			console.log("取消")
		},
		confirmFunc: function(res) {
			console.log(res)
			document.querySelector("#day").innerHTML = res.statrTime.time + ' 至 ' + res
				.endTime.time;
			loadData();
		}
	});
}

// 格式化时间函数
getNowTime = (format = 'YYYY-MM-DD HH:mm:ss') => {
	const now = new Date();
	const pad = num => num.toString().padStart(2, '0');

	const replacements = {
		'YYYY': now.getFullYear(),
		'MM': pad(now.getMonth() + 1),
		'DD': pad(now.getDate()),
		'HH': pad(now.getHours()),
		'mm': pad(now.getMinutes()),
		'ss': pad(now.getSeconds()),
		'SSS': pad(now.getMilliseconds(), 3)
	};

	return format.replace(/YYYY|MM|DD|HH|mm|ss|SSS/g, match => replacements[match]);
}

initTags = () => {
	const tags = new Tag({
		data: getTagsData(),
		container: '#tags',
		onChange: res => {
			index = res.key;
			loadData();
		}
	})
}

initListener = () => {
	$("#loading_layout").on('click', (e) => {
		loadData();
	});
	$("#pick_day").on('click', (e) => {
		if (dateSelect) {
			dateSelect.show();
		}
	});
}

loadData = () => {
	setLoadingState(0);
	$("#list").html('');
	setTimeout(() => {
		let res = [];
		setLoadingState(1);
		if (res.code == 200) {
			renderList(res.data);
		} else {
			setLoadingState(2);
		}
	}, 500)
}

getItem = (id) => {

}

renderList = mList => {
	$("#list").html('');
	if (ArrayEmpty(mList)) return setLoadingState(2, noData());

	mList.forEach(message => {
		const messageItem = document.createElement('div');
		messageItem.className = `message-item ${message.read ? '' : 'unread'}`;
		messageItem.dataset.id = message.id;
		messageItem.addEventListener('click', () => {
			getItem(message.id);
		})

		messageItem.innerHTML = `
	        <div class="message-icon">
	          
	        </div>
	        <div class="message-content">
	            <div class="message-title">
	                <span>${message.title}</span>
	               
	            </div>
	            <p class="message-text">${message.content}</p>
	            <div class="message-actions">
	                <button class="action-btn mark-btn">
	                    <i class="fas fa-${message.read ? 'envelope' : 'envelope-open'}"></i>
	                    ${message.read ? '标记未读' : '标记已读'}
	                </button>
	                <button class="action-btn delete-btn">
	                    <i class="fas fa-trash-alt"></i>
	                    删除
	                </button>
	            </div>
	        </div>
	    `;
		$("#list").append(messageItem);
	});
}
